10. Exercise: Set up Paint for Drawing
22 09 AAK Paint Intro SC V2
Android Developer Documentation
Exercise
In this exercise you are going to set up the Paint for drawing.
- In MyCanvasView.kt, at the top file level, define a constant for the stroke width.
private const val STROKE_WIDTH = 12f // has to be float
- At the class level of
MyCanvasView, define a variabledrawColorfor holding the color to draw with and initialize it with thecolorPaintresource you defined earlier.
private val drawColor = ResourcesCompat.getColor(resources, R.color.colorPaint, null)
- At the class level, below, add a variable
paintfor aPaintobject and initialize it as follows.
// Set up the paint with which to draw.
private val paint = Paint().apply {
color = drawColor
// Smooths out edges of what is drawn without affecting shape.
isAntiAlias = true
// Dithering affects how colors with higher-precision than the device are down-sampled.
isDither = true
style = Paint.Style.STROKE // default: FILL
strokeJoin = Paint.Join.ROUND // default: MITER
strokeCap = Paint.Cap.ROUND // default: BUTT
strokeWidth = STROKE_WIDTH // default: Hairline-width (really thin)
}